package scripts.finance;

import api.finance.AdminCheckApi;
import api.sea.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import util.AssertUtil;
import util.HttpClientUtils;
import util.ResponseData;
import util.TestDataUtil;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.*;

//录入应收数据
public class TestLoggData {
    String data_json;

    {
        try {
            data_json = FileUtils.readFileToString(new File("./data/money.json"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    JSONArray jsonArray=JSONArray.parseArray(data_json);
    List<Object> list= JSONObject.parseArray(jsonArray.toJSONString(),Object.class);
    private static final Logger logger = LoggerFactory.getLogger(TestLoggData.class);
    private String id;
    private String code;
    private static final StayPreplanApi stayPreplanApi=new StayPreplanApi();
    private final String number= RandomStringUtils.randomNumeric(8);
    private static final StuffApi stuffApi=new StuffApi();
    private static final StaySailApi staySailApi=new StaySailApi();
    private static final PutStorageApi putStorageApi=new PutStorageApi();

    @Test
    public void test_01() throws IOException, URISyntaxException {
        ResponseData responseData = LoginApi.login("admin", "qf123456?");
        String data = responseData.getBody().getJSONObject("data").getString("logistics-token");
        HttpClientUtils.build_header("logistics-token",data);
        HttpClientUtils.build_header("Content-Type","application/json");
    }
    @Test
    public void test_02() throws IOException, URISyntaxException {
        JSONArray data=new JSONArray();
        Map<String,String> map_data=new HashMap<>();
        map_data.put("cartons","1");
        map_data.put("length","1");
        map_data.put("width","1");
        map_data.put("mark","Tokyo  Hot");
        map_data.put("weight","1");
        data.add(map_data);
        Date date=new Date();
        SimpleDateFormat time=new SimpleDateFormat("HH-mm-ss");
        String s=time.format(date);
        String date_time="QFHY-"+s;
        ResponseData responseData= DelegationApi.delegation("TCY179310074272899092","COOLPAI",""+date_time,
                0,"",data,"2021-03-22",
                "LIANYUNGANG","CHICAGO","pingming","2021-04-06","3",""+date_time,
                "1","LWL"+s,
                "100","1","0","","","","",
                1,1,1,
                "","","US","","","","","Tokyo  Hot","",
                "",""+date_time,
                "6","1","0",0,"王晓亮","5409d88922bf4d5187f4",
                "7cd6188ea979499e892e","黄德挺",
                "7cd6188ea979499e892e","黄德挺","创实",
                "CU163032594897092663",2);
        this.id=responseData.getBody().getJSONObject("data").getString("id");
        this.code=responseData.getBody().getJSONObject("data").getString("entrustmentCode");

    }
    @Test
    public void test_03() throws IOException, URISyntaxException {
        ResponseData responseData= stayPreplanApi.stay_preplan(code,id,number,1,"1.00","1.00","LIANYUNGANG",
                "CHICAGO","2","4","11","2021-04-14","2021-04-14",1);
        logger.info("responseData={}",responseData);
    }
    @Test
    public void test_04() throws IOException, URISyntaxException {
        ResponseData responseData=stuffApi.stuff(number,"1111111","111111","2021-04-09",1);
        logger.info("responseData={}",responseData);
        logger.info("number={}",number);
    }
    @Test
    public void test_05() throws IOException, URISyntaxException {
        String forward="huodaidanhao"+number;
        ResponseData responseData=staySailApi.stay_sail(number,forward,"isf","2021-04-09","xaingguan","denggfuan",1);
        logger.info("responseData={}",responseData);
        logger.info("number={}",number);
    }
    //签收
    @Test
    public void test_06() throws IOException, URISyntaxException {
        ResponseData responseData= putStorageApi.put_storage("2",number,"2021-04-07","2021-04-07",1);
        logger.info("responseData={}",responseData);
        logger.info("number={}",number);
        AssertUtil.assertCommon(responseData,0,"成功");
    }
    @DataProvider
    public Object[][] build_data() throws IOException {
        for(int i=0;i<list.size();i++){
            Map<String,Object> map= (Map<String, Object>) list.get(i);
            List<Object> lists = (List<Object>) map.get("receivableRecordLists");
            for (int j=0;j< lists.size();j++){
                Map<String,Object> map_list= (Map<String, Object>) lists.get(j);
                map_list.put("entrustmentCode",code);
            }
        }
        FileUtils.writeStringToFile(new File("./data/money.json"), String.valueOf(list),"UTF-8");
        String key = "collectionNumber,receivableRecordLists";
        Object[][] test_data = TestDataUtil.build_data("money.json", key);
        logger.info("test_data={}", Arrays.deepToString(test_data));
        return test_data;
    }
    //录入应收
    @Test(dataProvider = "build_data")
    public static void test_07(String collectionNumber, JSONArray receivableRecordLists) throws IOException, URISyntaxException {
        ResponseData responseData = AdminCheckApi.hogen_affirm(collectionNumber, receivableRecordLists);
        logger.info("responseData={}", responseData.getBody().getJSONObject("data").getString("collectionNumber"));
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            data.add(responseData.getBody().getJSONObject("data").getString("collectionNumber"));
            System.out.println(data);
        }
    }

}
